home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / VideoToolboxSources / ChiSquare.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-04  |  1.3 KB  |  57 lines  |  [TEXT/KAHL]

  1. /*
  2. ChiSquare.c
  3. Copyright 1990 © Denis G. Pelli
  4. PChiSquare() calculates the significance level at which a fit may be rejected.
  5.  
  6. HISTORY:
  7. 1/7/84    dgp    wrote it in FORTRAN
  8. 8/9/89    dgp    translated it to C
  9. 4/5/90    dgp    tested it against tables in Abramowitz in Stegun. Works fine.
  10. 4/8/90    dgp    renamed file ChiSquare.c from PChiSquare.c
  11. 7/30/91    dgp    now use NAN defined in VideoToolbox.h
  12. 1/12/92    dgp    Renamed NormalPDF() to NormalPdf().
  13. */
  14. #include "VideoToolbox.h"
  15. #include <math.h>
  16.  
  17. double PChiSquare (double chiSquare,int n)
  18. /*
  19. Returns one minus the cumulative probability of the
  20. Chi-Square distribution for value chisq with n degrees of freedom.
  21. The formula is from Abramowitz and Stegun, Eqs. 26.4.4 and 26.4.5, pg 941.
  22. The formula is exact.
  23. Range: chisq>=0.0, n>=0,
  24. Returns zero if n is zero.
  25.  
  26. M. Abramowitz and I.A. Stegun (1964) Handbook of mathematical functions. Dover.
  27. */
  28. {
  29.     double x,a,P;
  30.     int i,ii;
  31.     
  32.     if(chiSquare<0.0 || n<0) return NAN;    /* return NAN to signal error */
  33.     x = sqrt(chiSquare);
  34.     if(n%2 != 0) {
  35.         /* n is odd */
  36.         P = 2.0*Normal(-x);
  37.         a = 2.0*NormalPdf(x)*x;
  38.         ii = (n-1)/2;
  39.         for(i=1;i<=ii;i++){
  40.             P += a;
  41.             a *= chiSquare/(2*i+1);
  42.         }
  43.     }
  44.     else {
  45.         /* n is even */
  46.         P = 0.0;
  47.         a = sqrt(2.0*PI)*NormalPdf(x);
  48.         ii = n/2;
  49.         for( i=1;i<=ii;i++){
  50.             P += a;
  51.             a *= chiSquare/(2*i);
  52.         }
  53.     }
  54.     return P;
  55. }
  56.  
  57.